const App = getApp();
import {
	url
} from '../../modelApi/url_config.js'
import { config } from '../../config.js'
Page({
	data: {
		statusBarHeight: "",
		statusBarPadding: "",
		roomTitle: "", //开创直播间标题
		timeData: "", // 时间
		picUrl: "", //  图片地址
		goodsIds: "",  //选择的商品id
		goodsCount: 0, // 选择商品的数量
	},
	onLoad: function (options) {
		this.setData({
			statusBarPadding: App.globalData.navTop,
			statusBarHeight: App.globalData.navHeight
		});
	},
	onShow: function () {
		App.getimLoad()
	},
	// 裁剪后的url
	returnUrl(imgurl) {
		console.log(imgurl,"imgurl")
		let that = this;
		wx.uploadFile({
			url: config.api_base_url + url.coverUploadUrl,
			filePath: imgurl,
			name: 'file',
			header: {
				'Content-Type': 'multipart/form-data',
				'accessToken': wx.getStorageSync('accessToken')
			},
			formData: {
				method: 'POST' //请求方式
			},
			success(res) {
				const dataObj = JSON.parse(res.data) //与wx.request不同的是，upload返回的是字符串格式，需要字符串对象化
				that.setData({
					picUrl: dataObj.data.url
				})
			},
			fail(err) {
				console.log(err)
			}
		})
	},
	/* 上传图片 */
	chooseImage(e) {
		wx.chooseImage({
			count: 1,
			sizeType: ['original', 'compressed'], //可选择原图或压缩后的图片
			sourceType: ['album', 'camera'], //可选择性开放访问相册、相机
			success: res => {
				const images = res.tempFilePaths
				wx.navigateTo({
				  url: `/pages/we-cropper/index?src=${images[0]}`,
				})
			}
		})
	},
	/* 切换时间 */
	timeToggle: function (e) {
		this.setData({
			timeData: e.detail
		})
	},
	formName(e) {
		const text = e.detail.value;
		this.setData({
			roomTitle: text
		})
	},
	/* 跳转商品选择 */
	goCheckShop() {
		wx.navigateTo({
			url: `/pages/checkShop/index`,
		})
	},
	creatTalkroom(roomTitle, id) {
        // 创建私有群
        let promise = App.globalData.wxTIM.createGroup({
            name: roomTitle,
            type: App.globalData.TIM.TYPES.GRP_CHATROOM,
            groupID: id.toString()
        });
        promise.then((imResponse) => { // 创建成功
						console.log(imResponse,"创建群1"); // 创建的群的资料
						wx.redirectTo({
							url: "/pages/live/sonpages/pusher/pusher?id=" + id
						});
						console.log(imResponse,"创建群2");
        }).catch((imError) => {
            console.warn('createGroup error:', imError); // 创建群组失败的相关信息
        });
    },
	/* 创建直播间 */
	setAuth: function () {
		// this.data.picUrl  = 'http://img.pconline.com.cn/images/upload/upc/tx/photoblog/1405/24/c7/34588443_1400945016422_mthumb.jpg'
		if (this.data.roomTitle == "" || this.data.picUrl == "") {
			wx.showToast({
				icon: "none",
				title: "请输入完整信息",
				duration: 1000
			})
			return;
		}
		if (this.data.goodsIds === "") {
			wx.showToast({
				icon: "none",
				title: "请选择直播宝贝",
				duration: 1000
			})
			return;
		}
		const params = {
			title: this.data.roomTitle,
			startTime: this.data.timeData,
			coverHUrl: this.data.picUrl,
			coverUrl: this.data.picUrl,
			goodsIds: this.data.goodsIds
		}
		wx.showLoading({
			title: "加载中..."
		});
		App.ajax(url.createRoomUrl, params, 'post').then(res => {
			wx.hideLoading();
			if (+res.code === 0) {
				this.creatTalkroom(this.data.roomTitle, res.data.id);
			}
		}).catch(err => {
			wx.hideLoading();
		})

	}
})